package com.example.demo.controller;

import com.example.demo.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class UploadController {

    @Autowired
    UploadService uploadService;

    @ResponseBody
    @PostMapping("/upload/file")
    public Object upload(@RequestParam("file") List<MultipartFile> multipartFile, HttpServletRequest request) throws IOException {

        if (multipartFile.isEmpty())
            return "文件上传有误";
        String dir = request.getParameter("dir");
        Map<String, List<String>> upload = uploadService.upload(multipartFile, dir);
        return upload;
    }
}
